package com.iatinspur.controller;

import com.alibaba.fastjson.JSONObject;
import com.iatinspur.listener.event.MyTestEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;

@Slf4j
@RestController
public class TestController {

    private String name ="黄飞" ;

    @Autowired
    private Environment env;



    @Autowired
    private ApplicationContext context;

    public String getPort() {
        return env.getProperty("server.port");
    }

    @GetMapping("/getResult")
    public String getResult(){
        String msg= MessageFormat.format("{0}({1})",this.name,getPort());
        MyTestEvent event = new MyTestEvent(msg);
        event.setLog(log);
        context.publishEvent(event);
        log.info("完成");
        return msg;
    }


    @ResponseBody
    @PostMapping("/getResult1")
    public JSONObject getResult1(@RequestBody JSONObject jsonObject){
        return jsonObject;
    }


    @GetMapping("/getResult2")
    public String getResult2(){
        return MessageFormat.format("{0}({1})",this.name,getPort());
    }
}
